package com.shou.controller.admin;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.shou.controller.MyController;
import com.shou.entity.PictureTextMsg;
import com.shou.service.impl.ActivityService;

@Controller("adminPictureTextController")
@RequestMapping("/admin/pictureText")
public class PictureTextController extends MyController{

	@Autowired
	ActivityService activityService;
	
	/**
	 * 图文信息列表
	 */
	@RequestMapping("/list")
	public String getPictureTextMsgList(Model model) {
		
		List<PictureTextMsg> list = activityService.getAllPictureText();
		model.addAttribute("list",list);
		return "/admin/pictureText/list";
	}
	
	/**
	 * 图文信息详情
	 */
	@RequestMapping("/edit")
	public String getPictureTextMsgInfo(Integer textId,Model model) {
		
		if(textId !=null){			
			PictureTextMsg info = activityService.getPictureTextMsg(textId);
			model.addAttribute("info",info);
		}
		return "/admin/pictureText/edit";
	}
	
	@RequestMapping("/save")
	public ResponseEntity<String> save(String description,String content,Integer id) {
		String msg = "保存成功";
		boolean status = true;
		if(StringUtils.isBlank(description)) {
			status = false;
			msg = "请输入描述";
		}
		else if(StringUtils.isBlank(content)) {
			status = false;
			msg = "请输入内容";
		}
		else {
			try{
				activityService.saveText(id, content, description);
			}
			catch(Exception e){
				e.printStackTrace();
				status = false;
				msg = "保存失败";
			}
		}
		return renderMsg(status, msg);
	}
}
